package com.lsw.jgyf.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.lsw.dialog.PubDialogUtil;
import com.lsw.jgyf.R;
import com.lsw.jgyf.adapter.PopupWindowSortAdapter;
import com.lsw.jgyf.model.JsonBean;
import com.lsw.jgyf.model.PopupWindowSortModel;
import com.lsw.jgyf.model.ShopModel;
import com.lsw.jgyf.ui.activity.MainActivity;
import com.lsw.jgyf.ui.base.fragment.BaseFragment;
import com.lsw.util.AppManager;
import com.lsw.util.DensityUtil;
import com.lsw.util.JsonFileReader;
import com.lsw.util.LogUtils;
import com.lsw.util.ToastUtil;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by lsw on 2018/7/8.
 */

public class RegisterDianyuanFragment extends BaseFragment<RegisterDianyuanFragmentPresenter> implements RegisterDianyuanFragmentContract.View, View.OnClickListener {
    @BindView(R.id.tv_register)
    TextView tv_register;
    PubDialogUtil dialogUtil;
    @BindView(R.id.ll_dianpu)
    LinearLayout ll_dianpu;
    @BindView(R.id.tv_dianpu)
    TextView tv_dianpu;
    @BindView(R.id.tv_fasong)
    TextView tv_fasong;
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_phone)
    TextView et_phone;
    @BindView(R.id.et_code)
    TextView et_code;
    @BindView(R.id.et_pass)
    TextView et_pass;
    @BindView(R.id.et_password)
    TextView et_password;
    //店铺ID
    String store_id;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_register_dianyuan;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        dialogUtil = new PubDialogUtil(mContext);
        tv_register.setOnClickListener(this);
        ll_dianpu.setOnClickListener(this);
        tv_fasong.setOnClickListener(this);
        mPresenter.getData();
    }

    @Override
    protected void lazyLoadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_register:
                mPresenter.register(store_id, et_name.getText().toString().trim(), et_code.getText().toString().trim(), et_pass.getText().toString().trim(), et_password.getText().toString().trim(), et_phone.getText().toString().trim());
                break;
            case R.id.ll_quyu:
                mPresenter.getAddress();
                break;
            case R.id.tv_fasong:
                mPresenter.sendCode(et_phone.getText().toString().trim(), "3");
                break;
            case R.id.ll_dianpu:
                if (listshop.size() == 0)
                    mPresenter.chooseShop();
                else initPopuptWindow(listshop, ll_dianpu);
                break;
        }
    }

    @Override
    public void setData() {
        for (int i = 0; i < 5; i++) {
            PopupWindowSortModel model = new PopupWindowSortModel();
            model.setId(i + "");
            model.setName("哈哈哈" + i);
            list1.add(model);
        }
    }

    @Override
    public void sendSucess() {
        startCountDown(60000);
    }

    @Override
    public void setAddress() {

    }

    List<PopupWindowSortModel> listshop = new ArrayList<>();

    @Override
    public void setShop(List<ShopModel> list) {
        for (int i = 0; i < list.size(); i++) {
            PopupWindowSortModel model = new PopupWindowSortModel();
            model.setName(list.get(i).getName());
            model.setId(list.get(i).getId() + "");
            listshop.add(model);
        }
        if (listshop.size() > 0) {
            initPopuptWindow(listshop, ll_dianpu);
        }
    }

    @Override
    public void registerSucess() {
        dialogUtil.showDialogtitleText("注册成功", "请耐心等待审核通过\n极果优分", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(mContext, MainActivity.class);
//                startActivity(intent);
                getActivity().finish();
            }
        }, false, "");
    }

    PopupWindow popupWindow;
    private List<PopupWindowSortModel> list1 = new ArrayList<>();
    PopupWindowSortAdapter popupWindowSortAdapter;
    View popupWindow_view;
    ListView listView;

    /**
     * 创建PopupWindow
     */
    protected void initPopuptWindow(List<PopupWindowSortModel> list1, View view_bottom) {
        // TODO Auto-generated method stub
        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        popupWindow_view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_sort, null,
                false);
        listView = popupWindow_view.findViewById(R.id.listview);
        popupWindowSortAdapter = new PopupWindowSortAdapter(mContext);
        popupWindowSortAdapter.setData(list1);
        listView.setAdapter(popupWindowSortAdapter);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            PopupWindowSortModel item = popupWindowSortAdapter.getItem(i);
            tv_dianpu.setText(item.getName());
            store_id = item.getId();
            popupWindow.dismiss();
        });
//        }
        popupWindow = new PopupWindow(popupWindow_view, DensityUtil.getWidth(mContext) - DensityUtil.dip2px(mContext, 60), LinearLayout.LayoutParams.WRAP_CONTENT, true);
//        // 设置动画效果
//        popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 点击其他地方消失
        if (Build.VERSION.SDK_INT < 24) {
            popupWindow.showAsDropDown(view_bottom);
        } else {
            // 适配 android 7.0
            int[] location = new int[2];
            view_bottom.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            popupWindow.showAtLocation(view_bottom, Gravity.NO_GRAVITY, DensityUtil.dip2px(mContext, 30), y + view_bottom.getHeight() + DensityUtil.dip2px(mContext, 5));
        }
        popupWindow.setOnDismissListener(() -> {
            popupWindow = null;
        });
    }

    private CountDownTimer countDownTimer;

    /**
     * 倒计时
     *
     * @param time
     */
    private void startCountDown(long time) {
        tv_fasong.setEnabled(false);
//        tv_fasong.setTextColor(mContext.getResources().getColor(R.color.gray));
        countDownTimer = new CountDownTimer(time, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tv_fasong.setText((millisUntilFinished / 1000) + "s后重新发送");
            }

            @Override
            public void onFinish() {
//                tv_fasong.setText(mContext.getResources().getString(R.string.verify_code));
//                tv_fasong.setBackground(mContext.getResources().getDrawable(R.drawable.round_blue_r));
//                tv_fasong.setTextColor(mContext.getResources().getColor(R.color.blue));
                tv_fasong.setText("重新发送");
                tv_fasong.setEnabled(true);
            }
        };
        countDownTimer.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null)
            countDownTimer.cancel();
    }
}
